#region Using directives
using System;
using SharpWind.Graphics;
using SharpWind.Resources.Shaders;
using SharpWind.Helpers;
using Microsoft.DirectX.Direct3D;
#endregion

namespace SharpWind.Scene
{
	public static class PostProcessingEffects
	{
		public static Shader Normal
		{
			get
			{
				Shader shader = Engine.Instance.Shaders.Load( "PostNormal.fx" );
				shader.Technique = "PostNormal";

				return shader;
			}
		}

		public static Shader Invert
		{
			get
			{
				Shader shader = Engine.Instance.Shaders.Load( "PostInvert.fx" );
				shader.Technique = "PostInvert";

				return shader;
			}
		}
	};

	public class PostProcessingManager
	{
		#region Fields

		private RenderTarget renderTarget = null;

		private ScreenQuad screenQuad = null;

		private Shader shader = PostProcessingEffects.Invert;

		#endregion

		#region Constructor

		public PostProcessingManager( int width, int height )
		{
			this.renderTarget	= new RenderTarget( width, height );
			this.screenQuad		= new ScreenQuad( width, height );
		}

		#endregion

		#region Methods

		public void Prepare( RenderCallback renderCallback )
		{
			this.renderTarget.RenderToTexture( renderCallback );
		}

		public void Render()
		{
			Engine.Instance.Graphics.Device.VertexDeclaration = VertexDeclarations.PostProcessing;

			this.shader.SetValue( "diffuseTexture", this.renderTarget.DxTexture );

			int numPasses = this.shader.Begin();
			for( int i = 0; i < numPasses; i++ )
			{
				this.shader.BeginPass( i );
				this.screenQuad.Render();
				this.shader.EndPass();
			}
			this.shader.End();
		}

		#endregion

		#region Properties

		public Shader Shader
		{
			get
			{
				return this.shader;
			}
			set
			{
				this.shader = value;
			}
		}

		#endregion
	}
}
